/**
 * Classname :WebUserDetailsService.java
 *
 * Version information: 1.0
 *
 * Date: Oct 28, 2013 ho.viet.duan create
 *
 */
package com.hlbbcsr.security;

import com.hlbbcsr.business.AccountManager;
import com.hlbbcsr.common.Constants.Role;
import com.hlbbcsr.domain.bean.Customer;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class WebUserDetailsService implements IWebUserDetailsService, InitializingBean {

	AccountManager accountManager = null;

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	/*
     * (non-Javadoc)
     * 
     * @see org.springframework.security.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
     */
	public synchronized UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
    		WebUserDetails user = null;
    		Customer customer = null;
    	try {
   			customer = accountManager.getCustomerByLoginId(userName);
    		if (customer == null) {        			
                System.out.println("User not found!!!");
    			throw new UsernameNotFoundException("User not found!!!");
            }
    		user = new AppWebUserDetails(customer, Role.MEMBER);

        } catch (UsernameNotFoundException ufe) {
            throw ufe;
        } catch (Exception e) {
            throw new UsernameNotFoundException("User not found!!!", e);
        }
        return user;
    }

    /*
     * (non-Javadoc)
     * 
     * @see kr.ac.edu.lab.web.security.IWebUserDetailsService#reloadUserByUsername(java.lang.String)
     */
    public void reloadUserByUsername(String userName) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
    }
}
